<template>
  <div>
    <el-dialog
      v-bind="$attrs"
      :title="form.id?'编辑角色':'新增角色'"
      :visible="dialogVisible"
      v-on="$listeners"
      @close="close"
    >
      <el-form ref="formRef" :model="form" :rules="rules" size="medium" label-width="100px">
        <el-form-item label="角色名称" prop="description">
          <el-input v-model="form.name" clearable :style="{width: '100%'}"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="name">
          <el-input v-model="form.description" :style="{width: '100%'}"></el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { addRoles, getRolesDetail, updateRoles } from '@/api/setting'
export default {
  components: {},
  inheritAttrs: false,
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        description: undefined,
        name: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '',
          trigger: 'blur'
        }],
        description: []
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    close() {
      this.form = {
        description: undefined,
        name: undefined
      }
      this.$emit('update:dialogVisible', false)
      this.$refs['formRef'].resetFields()
    },
    handelConfirm() {
      // 表单校验
      this.$refs['formRef'].validate(async valid => {
        if (!valid) return
        // 发请求
        if (this.form.id) {
          await updateRoles(this.form)
        } else {
          await addRoles(this.form)
        }
        // 提示成功
        this.$message.success('操作成功')
        // 通知父组件刷新页面
        this.$emit('success')
        // 刷新列表
        this.close()
      })
    },
    async getRolesDetail(id) {
      this.form = await getRolesDetail(id)
    }
  }
}

</script>
<style>
</style>
